<?php
/*
 * @Author: 李明
 * @Date: 2021-11-20 18:15:36
 * @LastEditTime: 2022-02-18 09:58:41
 * @LastEditors: 李明
 * @Description:
 * @FilePath: /CRM2.0-PHP/app/Http/Controllers/app/user/UserController.php
 * @山东壹零贰肆信息科技有限公司
 */

namespace App\Http\Controllers\app\user;

use App\Http\Controllers\common\LMReturn;
use App\Http\Controllers\Controller;
use App\Models\InterestRecord;
use App\Models\Order;
use App\Models\SystemBase;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use function PHPUnit\Framework\isNull;

class UserController extends Controller
{
    public function userSyDeital(Request $request)
    {
        // 处理数据
        try {
            //去验证token值
            $tokenInfo = json_decode($this->verifyTokenLm('user'), true);
            $channel = Request()->header('channel') ?? '1';
            $version = Request()->header('version') ?? '1';
            //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
            if ($tokenInfo['code'] == 400) {
                return LMReturn::tokenErr($tokenInfo['msg'], $tokenInfo['code']);
            }

            $user_id = $tokenInfo['user_id'];
            $user = User::find($user_id);
            $id_num = $user->id_number ?? '';
            if ($id_num != '') {
                $user_list = User::where('id_number', $id_num)->get();
                $user_id_list = [];
                foreach ($user_list as $user_c) {
                    $user_id_list[] = $user_c->id;
                }
            } else {
                $user_id_list = [$user->id];
            }
        } catch (\Throwable $th) {
            return LMReturn::Error('处理参数时发生错误', 'UCPHD01');
        }
        // 查询数据
        try {
            // 当前在途的本金和利息
            $sum_money = Order::whereIn('user_id', $user_id_list)
                ->whereIn('order_status', [3, 4])
                ->first(
                    array(
                        DB::raw('SUM(order_money) as money'),
                        // DB::raw('SUM(interest) as zt_interest')
                    )
                )->toArray();
            // 预期收益
            $sum_data = InterestRecord::where('is_exit', '2')
                ->whereHas('order.user', function ($query) use ($user_id_list) {
                    $query->whereIn('id', $user_id_list);
                })
                ->whereIn('status', [1, 2])
                ->first(
                    array(
                        // DB::raw('SUM(money) as money'),
                        DB::raw('SUM(interest) as zt_interest')
                    )
                )->toArray();
            // 累计收益
            $sum_interest = InterestRecord::where('is_exit', '2')
                ->whereHas('order.user', function ($query) use ($user_id_list) {
                    $query->whereIn('id', $user_id_list);
                })
                ->whereIn('status', [3])
                ->first(
                    array(
                        DB::raw('SUM(interest) as history_interest'),
                    )
                )->toArray();
            $system_data = SystemBase::find(1);
            $sum_data['money'] = $this->DealMoney($sum_money['money'], $channel, $version);
            $sum_data['zt_interest'] = $this->DealMoney($sum_data['zt_interest'], $channel, $version);
            $sum_data['history_interest'] = $this->DealMoney($sum_interest['history_interest'], $channel, $version);
            $sum_data['service_tel'] = $system_data->service_tel;
            $sum_data['user_name'] = $user->name;
            $user_icon = $user->icon;
            if ($user_icon == Null || $user_icon == '') {
                $user_icon = "https://api.guoyikonggu.com/images/nan.png";
            }
            $sum_data['user_icon'] = $user_icon;
            if ($user->unionid) {
                $is_binding = 1;
            } else {
                $is_binding = 2;
            }
            $sum_data['is_binding'] = $is_binding;
            return LMReturn::Success($sum_data);
        } catch (\Throwable $th) {
            return LMReturn::Error('查询时发生错误', 'UCPHD02');
        }
    }
    public static function DealMoney($money, $channel, $version)
    {

        if ($channel == '4') { // 安卓
            $money_str = '';
            if ($money >= 10000) {
                $money = $money / 10000;
                if ($money >= 10000) {
                    $money = $money / 10000;
                    $money_str  = round($money, 2) . '亿元';
                } else {
                    $money_str  = round($money, 2) . '万元';
                }
            } else {
                $money_str  = round($money, 2) . '元';
            }
            return $money_str;
        } else { //未知
            if ($version == '1') {
                $money_str = '';
                if ($money >= 10000) {
                    $money = $money / 10000;
                    $money = round($money, 2);
                    $money = $money * 10000;
                    $money_str = $money . '元';
                } else {
                    $money_str  = round($money, 2) . '元';
                }
            } else {
                $money_str = '';
                if ($money >= 10000) {
                    $money = $money / 10000;
                    if ($money >= 10000) {
                        $money = $money / 10000;
                        $money_str  = round($money, 2) . '亿元';
                    } else {
                        $money_str  = round($money, 2) . '万元';
                    }
                } else {
                    $money_str  = round($money, 2) . '元';
                }
            }
            return $money_str;
        }
    }
}
